package com.auxgroup.smarthome.qcloud.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.qcloud.bean.AlicloudConf;
import com.auxgroup.smarthome.qcloud.bean.FileModel;
import com.auxgroup.smarthome.web.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Service
public class AliCloudService {

    @Autowired
    private AlicloudConf alicloudConf;


    public ApiResponse<FileModel> uploadImg(MultipartFile file) throws IOException {
        OSSClient ossClient = new OSSClient(alicloudConf.getEndpoint(), alicloudConf.getAccessKeyId(), alicloudConf.getAccessKeySecret());
        String key = UUID.randomUUID().toString();
        String finalName = key + getSuffix(file.getOriginalFilename());
        LocalDate today = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String uploadPath = today.format(df) + "/" + BusinessConstant.UPLOAD_OSS_PREFIX_PATH_IMAGE.getMsg() + "/";

        //正式操作开始
        PutObjectResult result = ossClient.putObject(alicloudConf.getBucketName(),
                uploadPath + finalName,
                file.getInputStream());
        //这里可以添加回调函数
        // result.setCallbackResponseBody();
        //正式操作结束，关闭释放资源
        ossClient.shutdown();
        //自定义返回样式

        FileModel fileModel = new FileModel();

        StringBuffer stringBuffer = new StringBuffer(alicloudConf.getReturnUrlPrefix());
        stringBuffer.append('/' + uploadPath);
        stringBuffer.append(finalName);
        fileModel.setPath(stringBuffer.toString());

        fileModel.setName(finalName);
        fileModel.setSize(file.getSize());
        fileModel.setType(file.getContentType());
        return ApiResponse.ok(fileModel);
    }


    public ApiResponse<FileModel> uploadApk(MultipartFile file) throws IOException {
        OSSClient ossClient = new OSSClient(alicloudConf.getEndpoint(), alicloudConf.getAccessKeyId(), alicloudConf.getAccessKeySecret());
        String finalName =file.getOriginalFilename();
        String uploadPath =  "apkDir/";

        //正式操作开始
        PutObjectResult result = ossClient.putObject(alicloudConf.getBucketName(),
                uploadPath + finalName,
                file.getInputStream());
        //这里可以添加回调函数
        // result.setCallbackResponseBody();
        //正式操作结束，关闭释放资源
        ossClient.shutdown();
        //自定义返回样式

        FileModel fileModel = new FileModel();

        StringBuffer stringBuffer = new StringBuffer(alicloudConf.getReturnUrlPrefix());
        stringBuffer.append('/' + uploadPath);
        stringBuffer.append(finalName);
        fileModel.setPath(stringBuffer.toString());

        fileModel.setName(finalName);
        fileModel.setSize(file.getSize());
        fileModel.setType(file.getContentType());
        return ApiResponse.ok(fileModel);
    }



    /**
     * 返回文件名称后缀
     *
     * @param name
     * @return
     */
    private String getSuffix(String name) {
        int offset = name.lastIndexOf('.');
        if (offset != -1 && offset != (name.length() - 1)) {
            return name.substring(offset);
        }
        return "";
    }

}
